package com.example.tellwin.home.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tellwin.R;
import com.example.tellwin.home.bean.ActivitiesBean;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class PopularActivitiesAdapter extends RecyclerView.Adapter<PopularActivitiesAdapter.PopularActivitiesViewHolder> {

    List<ActivitiesBean> beans = new ArrayList<>();
    Context context;
    private OnItemPopularActivitiesClickListener itemPopularActivitiesClickListener;

    public void setBeans(List<ActivitiesBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    public void setItemPopularActivitiesClickListener(OnItemPopularActivitiesClickListener itemPopularActivitiesClickListener) {
        this.itemPopularActivitiesClickListener = itemPopularActivitiesClickListener;
    }

    @NonNull
    @Override
    public PopularActivitiesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new PopularActivitiesViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popular_activities, parent, false));
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    @Override
    public void onBindViewHolder(@NonNull PopularActivitiesViewHolder holder, int position) {
        ActivitiesBean bean = beans.get(position);
        if (position % 4 == 0) {
            holder.lineIv.setImageResource(R.color.act_line_color_1);
            holder.itemPopularActivitiesView.setBackgroundResource(R.drawable.pick_concers_shape);
        } else if (position % 4 == 1) {
            holder.lineIv.setImageResource(R.color.act_line_color_2);
            holder.itemPopularActivitiesView.setBackgroundResource(R.drawable.light_green_concers_shape);
        } else if (position % 4 == 2) {
            holder.lineIv.setImageResource(R.color.act_line_color_3);
            holder.itemPopularActivitiesView.setBackgroundResource(R.drawable.light_yellow_corners_shape);
        } else if (position % 4 == 3) {
            holder.lineIv.setImageResource(R.color.act_line_color_4);
            holder.itemPopularActivitiesView.setBackgroundResource(R.drawable.light_blue_corners_shape);
        }
        if(bean.getActivityStatus() == 1) {
            holder.activitiesTypeTv.setBackgroundResource(R.mipmap.act_type_icon_1);
            holder.activitiesTypeTv.setText("报名中");
        }else if(bean.getActivityStatus() ==2){
            holder.activitiesTypeTv.setBackgroundResource(R.mipmap.act_type_icon_2);
            holder.activitiesTypeTv.setText("投票中");
        }else {
            holder.activitiesTypeTv.setBackgroundResource(R.mipmap.act_type_icon_3);
            holder.activitiesTypeTv.setText("结束");
        }
        holder.activityTitleTv.setText(bean.getActivityName());
        holder.participantTv.setText(context.getResources().getString(R.string.participant , CommonUtils.getParticipateStr(bean.getParticipateType())));

        holder.registrationPopulationTv.setText(context.getResources().getString(R.string.registration_population , bean.getParticipateCount()+""));
        holder.registrationTimeTv.setText(context.getResources().getString(R.string.registration_time , bean.getParticipateTime()));
        GlideUtils.LoadImage(context , bean.getActivityPic() ,holder.activityIv);
        holder.itemPopularActivitiesView.setOnClickListener(v -> {
            if(itemPopularActivitiesClickListener!=null){
                itemPopularActivitiesClickListener.itemPopularActivitiesClick(position ,bean);
            }
        });
    }

    class PopularActivitiesViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemPopularActivitiesView;
        ImageView lineIv;
        ImageView activityIv;
        TextView activitiesTypeTv;
        TextView activityTitleTv;
        TextView participantTv;
        TextView registrationPopulationTv;
        TextView registrationTimeTv;

        public PopularActivitiesViewHolder(@NonNull View itemView) {
            super(itemView);
            itemPopularActivitiesView = (RelativeLayout) itemView.findViewById(R.id.item_popular_activities_view);
            lineIv = (ImageView) itemView.findViewById(R.id.line_iv);
            activityIv = (ImageView) itemView.findViewById(R.id.activity_iv);
            activitiesTypeTv = (TextView) itemView.findViewById(R.id.activities_type_tv);
            activityTitleTv = (TextView) itemView.findViewById(R.id.activity_title_tv);
            participantTv = (TextView) itemView.findViewById(R.id.participant_tv);
            registrationPopulationTv = (TextView) itemView.findViewById(R.id.registration_population_tv);
            registrationTimeTv = (TextView) itemView.findViewById(R.id.registration_time_tv);
        }
    }

    public interface OnItemPopularActivitiesClickListener{
        void itemPopularActivitiesClick(int position , ActivitiesBean activitiesBean);
    }
}
